package jsyf.oa.web.system;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.system.SystemFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: 崔程鹏
 * @creat: 2017/11/24 0024 14:24
 * description:
 * 系统控制层
 */
@Api(value = "系统接口", description = "系统接口")
@Controller
@RequestMapping("/system")
public class SystemController {

    @Resource
    private SystemFacade systemFacade;

    /**
     * @description: 获取当前时间
     * @author: @崔程鵬 @cuichengpeng
     * @creat: 11:40  2017/12/6 0006
     * @since: V1.0.0
     * @see jsyf.oa.web
     */
    @RequestMapping(value = "/getCurrentTime.json", method = RequestMethod.POST)
    public void getCurrentTime(HttpServletResponse response) {
        String result = systemFacade.getCurrentTime();
        JSONObject dateTime = new JSONObject();
        dateTime.put("dateTime", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(dateTime));
    }


    /**
     * @description: 查询组织结构树
     * @author: @崔程鵬 @cuichengpeng
     * @creat: 11:40  2017/12/6 0006
     * @since: V1.0.0
     * @see jsyf.oa.web
     */
    @RequestMapping(value = "/getOrgTreeNodesByUserId.json", method = RequestMethod.POST)
    public void getOrgTreeNodesByUserId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        List<JSONObject> result = systemFacade.getOrgTreeNodesByUserId(jsonParam.getString("userId"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }


    /**
     * @description: 查询视图用户数据-分页-条件（公司，部门，职位，key模糊查询人员和电话）
     * @author: @崔程鵬 @cuichengpeng
     * @creat: 11:41  2017/12/6 0006
     * @since: V1.0.0
     * @see jsyf.oa.web
     */
    @RequestMapping(value = "/getUserViewBySqlWhere.json", method = RequestMethod.POST)
    public void getUserViewBySqlWhere(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        JSONObject records;
        String requestPage;
        String pageSize;
        if (jsonParam.containsKey("requestPage") && jsonParam.containsKey("pageSize")) {
            requestPage = jsonParam.getString("requestPage");
            pageSize = jsonParam.getString("pageSize");

            records = this.systemFacade.getUserViewBySqlWhere(jsonParam, Integer.parseInt(requestPage), Integer.parseInt(pageSize));
        } else {
            requestPage = "1";
            pageSize = Integer.MAX_VALUE + "";
            records = this.systemFacade.getUserViewBySqlWhere(jsonParam, Integer.parseInt(requestPage), Integer.parseInt(pageSize));
        }
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }

    }

    /**
     * 获取所有公司-所有部门-所有人员树形结构
     */
    @ApiOperation(
            value = "获取所有公司-所有部门-所有人员树形结构",
            notes = "获取所有公司-所有部门-所有人员树形结构",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonData", value = "", dataType = "参数类型（string）", paramType = "参数类型（json）")
    })
    @ResponseBody
    @RequestMapping(value = "/getCDUAll.json", method = RequestMethod.POST)
    public void getCDUAll(HttpServletRequest request, HttpServletResponse response) {
        List<JSONObject> records = this.systemFacade.getCDUAll();
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
    /**
     * 获取所有公司
     */
    @ApiOperation(
            value = "获取所有公司",
            notes = "获取所有公司",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonData", value = "", dataType = "参数类型（string）", paramType = "参数类型（json）")
    })
    @ResponseBody
    @RequestMapping(value = "/getCompanyTree.json", method = RequestMethod.POST)
    public void getCompanyTree(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = this.systemFacade.getCompanyTree(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getOrgTreeByUserId.json", method = RequestMethod.POST)
    public void getOrgTreeByUserId(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = this.systemFacade.getOrgTreeByUserId(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getCompanyTreeForXSTZ.json", method = RequestMethod.POST)
    public void getCompanyTreeForXSTZ(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = this.systemFacade.getCompanyTreeForXSTZ(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
}
